﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WebDUPlugIn.Crawlers
{
    class CrawlDictionary
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static Dictionary<string,bool> _urlvisitedList = new Dictionary<string, bool>();
        public static void MarkasQueuedUri(Uri uri)
        {

            if( IsQueued(uri) )
            {
                return;
            }

            // Insert Now 
            lock (_urlvisitedList)
            {
                _urlvisitedList[uri.ToString()] = true;
            }

        }

        // Check to see if node already Visited.. 

        public static bool IsQueued(Uri uri)
        {
            bool retVal;
            lock (_urlvisitedList)
            {
                 retVal = _urlvisitedList.ContainsKey(uri.ToString());
            }
            return retVal;
        }

        public static void DumpDictionarytoFile()
        {

            string dir = @"C:\Crawler";

            if( !Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            string fileName = string.Format("{0}\\CrawlOut.Txt", dir);
            if( File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            try
            {
                FileStream fs = File.Create(fileName);
                var sw = new StreamWriter(fs);
                lock (_urlvisitedList)
                {
                    foreach (KeyValuePair<string, bool> pair in _urlvisitedList)
                    {
                        sw.WriteLine(" URL :   {0}", pair.Key);
                    }
                }
                sw.Close();
                
            }
            catch(IOException e)
            {
                log.FatalFormat(" Exception during report generation.. {0} ",e.ToString());
            }
        }
    }
}
